<?php

namespace Admin\Controller;

use Common\Controller\AdminbaseController;
use Common\Model\LiveWemeModel;
use Common\Model\LiveFriendCircleModel;
use Common\Model\LiveWemeNicknameModel;

/**
 * Description of WemeController
 *
 * @author
 */
class WemeCompanyController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->_mod = D('LiveWeme');
        $this->user_admin_model = D("Common/AdminUser");
        $this->tag_model = D("Common/LiveTag");
        $this->assign('status', [100 => '未发送', 1 => '已发布']);

        $admin_id = $_SESSION["ADMIN_ID"];
        ($admin_id==1) and redirect(U("admin/Wemeext/index"));
        $this->company_id = $this->user_admin_model->where(array('admin_id' => $admin_id))->getField('company_id');
        $tag_list = $this->tag_model->where(array('company_id' => $this->company_id))->field('id,tag_name')->select();
        $tags = array_combine($this->_array_column($tag_list, 'id'), $this->_array_column($tag_list, 'tag_name'));
        $this->assign('tags', $tags);
    }

    function index() {
        $map = $this->_search();
        $map['company_id'] = $this->company_id;
        $mod = $this->_mod;
        if (!empty($mod)) {
            $result = $this->_list($mod, $map);
            $this->assign('page', $result['page']);
            $list = $result['list'];
            foreach ($list as $key => $val) {
                $list[$key]['nickname'] = D('LiveWemeNickname')->where(array('mirrtalkID' => $val['mirrtalkid']))->getField('nickname');
                $list[$key]['tag'] = unserialize($val['tags']);
            }
//            return print_r($list);
            $this->assign('list', $list);
            $this->display();
        }
    }

    public function send_weme() {
        if ($this->_request('action')) {
            if ($this->_request('type') == 1) {
                $id = $this->_request('id');
                !$this->_request('cover') && $this->ajaxReturn(array("status" => 0, "info" => "未上传图片！"));
                $weme = $this->_mod->where(array('id' => $id))->find();
                $weme_nickname = D('LiveWemeNickname')->where(array('mirrtalkID' => $weme['mirrtalkid']))->find();
                !$weme_nickname['nickname'] && $this->ajaxReturn(array("status" => 0, "info" => "昵称不能为空"));
                $data['author'] = $weme_nickname['nickname'];
                $data['avatar'] = $weme_nickname['url'];
                $data['authorid'] = $weme_nickname['id'];
                $data['subject'] = $this->_request('content');
                $data['dateline'] = time();
                $data['status'] = 32;
                $data['url'] = $weme['url'];
                $data['icon'] = $this->_request('cover');
                $res = D('LiveFriendCircle')->add($data);
                if ($weme['status'] == 0) {
                    $this->_mod->where(array('id' => $id))->save(array('status' => 2));
                }
                if ($weme['status'] == 1) {
                    $this->_mod->where(array('id' => $id))->save(array('status' => 3));
                }
                $res ? $this->ajaxReturn(array("status" => 1, "info" => "发布成功")) : $this->ajaxReturn(array("status" => 0, "info" => "发布失败！"));
                return;
            }

            if ($this->_request('type') == 6) {
                $id = $this->_request('id');
                $gallery = D('LiveGallery')->where(array('id' => $id))->find();
                $weme_nickname = D('LiveWemeNickname')->where(array('id' => rand(2, 15)))->find();
                $data['author'] = $gallery['nickname'];
                $data['avatar'] = $weme_nickname['url'];
                $data['authorid'] = $weme_nickname['id'];
                $data['subject'] = $this->_request('content');
                $data['dateline'] = time();
                $data['status'] = 32;
                $data['url'] = $gallery['url'];
                $res = D('LiveFriendCircle')->add($data);
                if ($gallery['status'] == 0) {
                    D('LiveGallery')->where(array('id' => $id))->save(array('status' => 2));
                }
                if ($gallery['status'] == 1) {
                    D('LiveGallery')->where(array('id' => $id))->save(array('status' => 3));
                }
                $res ? $this->ajaxReturn(array("status" => 1, "info" => "发布成功")) : $this->ajaxReturn(array("status" => 0, "info" => "发布失败！"));
                return;
            }
            if ($this->_request('type') == 0) {
                $id = $this->_request('id');
                $weme = $this->_mod->where(array('id' => $id))->find();
                if ($weme['status'] == 0) {
                    $this->_mod->where(array('id' => $id))->save(array('status' => 1));
                }
                if ($weme['status'] == 2) {
                    $this->_mod->where(array('id' => $id))->save(array('status' => 3));
                }
                return;
            }
            if ($this->_request('type') == 3) {
                $id = $this->_request('id');
                $gallery = D('LiveGallery')->where(array('id' => $id))->find();
                if ($gallery['status'] == 0) {
                    D('LiveGallery')->where(array('id' => $id))->save(array('status' => 1));
                }
                if ($gallery['status'] == 2) {
                    D('LiveGallery')->where(array('id' => $id))->save(array('status' => 3));
                }
                return;
            }
        }
        if ($this->_request('type') == 0) {
            $id = $this->_request('id');
            $weme = $this->_mod->where(array('id' => $id))->find();
            $this->assign('videos', $weme['url']);
        }
        if ($this->_request('type') == 3) {
            $id = $this->_request('id');
            $gallery = D('LiveGallery')->where(array('id' => $id))->find();
            $this->assign('files', $gallery['url']);
        }
        $this->assign('id', $this->_request('id'));
        $this->assign('type', $this->_request('type'));
        $this->display();
    }

    public function send_list() {
        $where['status'] = array('exp', ' IN (1,3) ');
        $where['category'] = array('exp', ' IN (3,4,5) ');
        $live_list = D('Live')->where($where)->select();
        echo json_encode($live_list);
    }

    public function delete() {
        $map['id'] = $this->_request('id');
        $res = $this->_mod->where($map)->delete();
        $res ? $this->ajaxReturn(array("status" => 1, "info" => "删除成功")) : $this->ajaxReturn(array("status" => 0, "info" => "删除失败！"));
    }

    public function tag_set() {
        $mod = $this->tag_model;
        $id = $this->_request('id');
        if (!empty($mod)) {
            if ($this->_request('type') == 1) {
                $data = $_POST["data"];
                $data = json_decode($data, true);
                $data = serialize($data);
                $res = $this->_mod->where(array('id' => $id))->save(array('tags' => $data));
                $res ? $this->ajaxReturn(array("status" => 1, "info" => "设置成功")) : $this->ajaxReturn(array("status" => 0, "info" => $data));
            }
            $where['company_id'] = $this->company_id;
            $list = $mod->where($where)->select();
            $weme = $this->_mod->where(array('id' => $id))->find();
            $tags=$weme['tags'];
            $tags = unserialize($tags);
            foreach ($list as $key => $value) {
                if (in_array($value['id'], $tags)) {
                        $list[$key]['select'] =1;
                }
            }
            $this->assign('list', $list);
            $this->assign('id', $id);
            $this->display();
        }
    }

    protected function _list($model, $map = array(), $sort_by = '', $order_by = '', $field_list = '*', $pagesize = 20) {
        //排序
        $mod_pk = $model->getPk();
        if ($this->_request("sort", 'trim')) {
            $sort = $this->_request("sort", 'trim');
        } else if (!empty($sort_by)) {
            $sort = $sort_by;
        } else if ($this->sort) {
            $sort = $this->sort;
        } else {
            $sort = $mod_pk;
        }
        if ($this->_request("order", 'trim')) {
            $order = $this->_request("order", 'trim');
        } else if (!empty($order_by)) {
            $order = $order_by;
        } else if ($this->order) {
            $order = $this->order;
        } else {
            $order = 'DESC';
        }
        $result = array();
        //如果需要分页
        if ($pagesize) {
            $count = $model->where($map)->count($mod_pk);
            $pager = $this->page($count, $pagesize);
        }
        $select = $model->field($field_list)->where($map)->order($sort . ' ' . $order);
        $this->list_relation && $select->relation(true);
        if ($pagesize) {
            $select->limit($pager->firstRow . ',' . $pager->listRows);
            $page = $pager->show('Admin');
            $result['page'] = $page;
            $this->assign("page", $page);
        }
        $list = $select->select();
        $result['list'] = $list;
        $this->assign('list_table', true);
        return $result;
    }

    protected function _search() {
        $map = array();
        ($author = $this->_request('author', 'trim')) && $map['mirrtalkID'] = array('IN', $this->_get_uids($author, 'nickname'));
        ($tags = $this->_request('tags', 'trim')) && $map['id'] = array('IN', $this->_get_tag_uids($tags));
        if ($this->_request('status')) {
            $status = ($this->_request('status') != 100) ? $this->_request('status') : 0;
            if ($status == 0) {
                $map['status'] = $status;
            } else {
                $map['status'] = array('exp', ' IN (1,2,3) ');
            }
        }
        ($time_start = $this->_request('stime', 'trim')) && $map['mtime'] = array(array('egt', strtotime($time_start)), array('lt', strtotime($this->_request('etime', 'trim'))));
        $this->assign('search', array(
            'author' => $this->_request('author'),
            'stime' => $this->_request('stime'),
            'etime' => $this->_request('etime'),
            'status' => $this->_request('status'),
            'tags' => $this->_request('tags'),
        ));
        return $map;
    }

    protected function _request($parama) {
        return I($parama);
    }

    protected function _get_uids($keyword, $field) {
        $where[$field] = array('like', '%' . $keyword . '%');
        $uidarr = D('LiveWemeNickname')->where($where)->field('mirrtalkid')->select();
        $_idarr = array();
        foreach ($uidarr as $v) {
            $_idarr[] = $v['mirrtalkid'];
        }
        return implode(',', $_idarr);
    }

    protected function _get_tag_uids($tag_id) {
        $list = $this->_mod->select();
        $_idarr = array();
        foreach ($list as $key => $val) {
            $tags = unserialize($val['tags']);
            if (in_array($tag_id, $tags)) {
                $_idarr[] = $val['id'];
            }
        }
        return implode(',', $_idarr);
    }

    protected function _array_column($input, $columnKey, $indexKey = NULL) {
        $columnKeyIsNumber = (is_numeric($columnKey)) ? TRUE : FALSE;
        $indexKeyIsNull = (is_null($indexKey)) ? TRUE : FALSE;
        $indexKeyIsNumber = (is_numeric($indexKey)) ? TRUE : FALSE;
        $result = array();

        foreach ((array) $input AS $key => $row) {
            if ($columnKeyIsNumber) {
                $tmp = array_slice($row, $columnKey, 1);
                $tmp = (is_array($tmp) && !empty($tmp)) ? current($tmp) : NULL;
            } else {
                $tmp = isset($row[$columnKey]) ? $row[$columnKey] : NULL;
            }
            if (!$indexKeyIsNull) {
                if ($indexKeyIsNumber) {
                    $key = array_slice($row, $indexKey, 1);
                    $key = (is_array($key) && !empty($key)) ? current($key) : NULL;
                    $key = is_null($key) ? 0 : $key;
                } else {
                    $key = isset($row[$indexKey]) ? $row[$indexKey] : 0;
                }
            }

            $result[$key] = $tmp;
        }

        return $result;
    }

}
